<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    

    <script>



let arr = [

    { comment_id: 76, user_id: 42, comment_date: "05-14", comment_content: "你好", parent_id: null },
    { comment_id: 78, user_id: 42, comment_date: "05-14", comment_content: "你也好", parent_id: "76" },
    { comment_id: 79, user_id: 42, comment_date: "05-14", comment_content: "你也是", parent_id: "78" },
    { comment_id: 80, user_id: 42, comment_date: "05-14", comment_content: "你也一样", parent_id: "79" },
    { comment_id: 81, user_id: 42, comment_date: "05-14", comment_content: "大家都好", parent_id: "80" },
    { comment_id: 82, user_id: 391, comment_date: "05-14", comment_content: "大家都一样", parent_id: "81" },
    { comment_id: 85, user_id: 487, comment_date: "05-14", comment_content: "我也觉得是", parent_id: "82" },
    { comment_id: 86, user_id: 487, comment_date: "05-14", comment_content: "不太一样", parent_id: "81" },
    { comment_id: 87, user_id: 391, comment_date: "05-14", comment_content: "哈哈哈", parent_id: "85" },
    { comment_id: 90, user_id: 548, comment_date: "05-15", comment_content: "D", parent_id: null },
    { comment_id: 93, user_id: 578, comment_date: "05-16", comment_content: "11111", parent_id: null },
    { comment_id: 94, user_id: 578, comment_date: "05-16", comment_content: "111111", parent_id: null },
    { comment_id: 95, user_id: 578, comment_date: "05-16", comment_content: "大家的bilibili做的怎么样了", parent_id: null },
    { comment_id: 103, user_id: 662, comment_date: "05-16", comment_content: "xcczzczxc", parent_id: "95" },
    { comment_id: 111, user_id: 229, comment_date: "05-17", comment_content: "giaogiao", parent_id: "78" },
    { comment_id: 112, user_id: 229, comment_date: "05-17", comment_content: "有点懵", parent_id: "95" },
    { comment_id: 113, user_id: 229, comment_date: "05-17", comment_content: "你好啊", parent_id: "79" },
    { comment_id: 114, user_id: 229, comment_date: "05-17", comment_content: "你好", parent_id: "86" },
    { comment_id: 115, user_id: 229, comment_date: "05-17", comment_content: "好好", parent_id: "114" },
    { comment_id: 116, user_id: 229, comment_date: "05-17", comment_content: "2020-8-17,加油", parent_id: null },
    {comment_id: 117, user_id: 2537, comment_date: "06-10", comment_content: "加油，奥利给", parent_id:"116"}
]





//添加 child 内容。 递归版本
function fn(temp){
        let arr1 = []

        for(var i = 0; i<arr.length;i++){
            if(arr[i].parent_id == temp){
                arr1.push(arr[i])   //评论的内容，评论级别由参数 temp 决定，为null 时就保存一级评论内容，为1 时就继续保存二级的内容
                
                //这里传参。当i=0时。传入索引值为0的comment_id，然后执行fn方法。
                //执行fn方法后，在判断时，如果当前的parent_id（回复id） == 传入的 comment_id,那么把内容保存到arr1
                //最后在这里继续添加child,嵌套child 数据。（一级评论添加下一级别的数据）
                arr[i].child = fn(arr[i].comment_id)
            }
            
        }
        return arr1
}

const res = fn(null)
console.log(res);




//添加 child 内容。 回调地狱版本
    /* for(var i = 0; i<arr.length;i++){
        if(arr[i].parent_id == null){
            arr1.push(arr[i])//一级评论的内容

            for(var j = 0; j<arr.length;j++){
                if(arr[i].comment_id == arr[j].parent_id){
                    arr[i].child = [ arr[j] ]//一级评论里面存在着二级评论的内容
                    
                    for(var p = 0; p<arr.length;p++){
                        if(arr[j].comment_id == arr[p].parent_id){
                            arr[j].child = [ arr[p] ]//二级评论里面存在着三级评论的内容


                        }
                    }

                }
            }

        }

    } 
    */
</script>
</body>
</html>